/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.eit.ntc.lmsbackend.struts.action;


import com.eit.ntc.lmsbackend.struts.form.LMSForm;
import com.eit.ntc.lmsbackend.utils.RoleUtils;
import com.eit.ntc.struts.action.AbstractWebFlowAction;

import com.eit.toschoolonline.lms.jdo.LearningItemM;
import com.eit.toschoolonline.lms.jdo.LessonM;
import com.eit.toschoolonline.lms.jdo.SubjectM;

import com.eit.toschoolonline.lms.service.LessonService;
import com.eit.toschoolonline.lms.service.SubjectService;
import java.util.ArrayList;
import java.util.List;
import org.springframework.validation.Errors;
import org.springframework.webflow.execution.Event;
import org.springframework.webflow.execution.RequestContext;

/**
 *
 * @author chanon
 */
public class SubjectAction extends AbstractWebFlowAction{
    private SubjectService subjectService;
    private LessonService lessonService;
    
     public Event MainSubject(RequestContext context) throws Exception {
         LMSForm lForm = (LMSForm) getFormObject(context);
           lForm.setSubjectM(null);
           lForm.setErrorMode("");
          
           SubjectService service = getSubjectService();
           lForm.setSubjectList(service.getSubjectAll());     
          return success();
        }
    public Event AddSubject(RequestContext context) throws Exception {
         LMSForm lForm = (LMSForm) getFormObject(context);
        
              lForm.setErrorMode("");
              lForm.setSubjectM(null);
              lForm.setEditMode("");
              lForm.setSubjectCode("");
              lForm.setSubjectName("");   
             return new Event(this, "success");
        } 
     public Event DelSubject(RequestContext context) throws Exception {
             
              LMSForm lForm = (LMSForm) getFormObject(context);        
              SubjectService service = getSubjectService();
              LessonService lService = getLessonService();
              
              int i = Integer.parseInt(lForm.getInputID());
              SubjectM s = service.getSubject(i);
              
              List lessons = lService.getLessonBySubject(s.getSubjectCode());
              if (lessons !=null && lessons.size()==0){
                  s.setStatus(SubjectM.DELETE_STATUS);
                  service.deleteEntranceSubject(s);
              } else {
                  Errors error = getFormErrors(context);
                  error.reject("errors.LessonFound");
                  return error();
              }
             return new Event(this, "success");
        } 
      public Event EditSubject(RequestContext context) throws Exception {
         LMSForm lForm = (LMSForm) getFormObject(context);
         String subjectID = getHttpServletRequestParameter(context,"id");
          SubjectService service = getSubjectService();
           SubjectM subject = null;
             subject = service.getSubject(Integer.parseInt(subjectID)); 
              lForm.setErrorMode("");
              lForm.setEditMode("");      
              lForm.setSubjectCode(subject.getSubjectCode());
              lForm.setSubjectName(subject.getSubjectName());   
              lForm.setSubjectM(subject);
             return new Event(this, "success");
        }
       public Event SaveSubject(RequestContext context) throws Exception {
         LMSForm lForm = (LMSForm) getFormObject(context);
           SubjectM s = new SubjectM();      
           lForm.setErrorMode("");
           s.setSubjectCode(lForm.getSubjectCode());
           s.setSubjectName(lForm.getSubjectName());
           s.setStatus(SubjectM.ENABLE_STATUS);
           SubjectService service = getSubjectService();
           if(service.getEnableSubjectByCode(lForm.getSubjectCode())==null){
           s = service.createSubject(s);
           // include create entrance-lesson in subject-spring-bean


             return new Event(this, "success");}
           else {
               lForm.setErrorMode("มี SubjectCode นี้อยู่แล้วกรุณาตรวจสอบใหม่อีกครั้ง");
               return new Event(this, "fail"); }
        } 
        public Event SaveEditSubject(RequestContext context) throws Exception {
         LMSForm lForm = (LMSForm) getFormObject(context);
           SubjectM s = lForm.getSubjectM();      
           s.setSubjectCode(lForm.getSubjectCode());
           s.setSubjectName(lForm.getSubjectName());
           SubjectService service = getSubjectService();
           service.updateSubject(s);
           // include create entrance-lesson in subject-spring-bean
           
             return new Event(this, "success");
        } 
       
        public Event CancelSubject(RequestContext context) throws Exception {
         LMSForm lForm = (LMSForm) getFormObject(context);
                
             return new Event(this, "success");
        }

    public SubjectService getSubjectService() {
        return subjectService;
    }

    public void setSubjectService(SubjectService subjectService) {
        this.subjectService = subjectService;
    }

    public LessonService getLessonService() {
        return lessonService;
    }

    public void setLessonService(LessonService lessonService) {
        this.lessonService = lessonService;
    }

   
    

}
